<template>
  <div class="gi-tag" :class="getClassName" @click="handleClick">
    <slot></slot>
    <span class="gi-tag-close-btn" v-if="closable" @click="handleClose"><icon-close class="close-icon" /></span>
  </div>
</template>

<script setup lang="ts" name="GiTag">
import { computed, type PropType } from 'vue'

type GiTagType = 'dark' | 'light' | 'outline' | 'light-outline'
type GiTagStatus = 'primary' | 'success' | 'warning' | 'danger' | 'info'
type GiTagSize = 'mini' | 'small' | 'large'

interface Props {
  type: GiTagType
  status: GiTagStatus
  size: GiTagSize
  closable: boolean
}

const props = withDefaults(defineProps<Props>(), {
  type: 'light',
  status: 'primary',
  size: 'small',
  closable: false
})

const getClassName = computed<string>(() => {
  return `gi-tag-${props.type} gi-tag-size-${props.size} gi-tag-status-${
    props.status === 'info' ? 'gray' : props.status
  }`
})

const emit = defineEmits(['click', 'close'])

const handleClick = () => {
  emit('click')
}

const handleClose = () => {
  emit('close')
}
</script>

<style lang="scss" scoped>
.gi-tag {
  display: inline-flex;
  padding: 0 8px;
  // padding-top: 1px;
  height: 20px;
  font-size: 12px;
  line-height: 1;
  border-radius: 3px;
  justify-content: center;
  align-items: center;
  white-space: nowrap;
  box-sizing: border-box;
  cursor: pointer;
}

.gi-tag-close-btn {
  position: relative;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  margin-left: 4px;
  .close-icon {
    width: 12px;
    height: 12px;
    z-index: 9;
  }
  &::before {
    width: 16px;
    height: 16px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    content: '';
    position: absolute;
    display: block;
    box-sizing: border-box;
    background-color: transparent;
    border-radius: var(--border-radius-circle);
    transition: background-color 0.1s cubic-bezier(0, 0, 1, 1);
  }
}

$status: primary, success, warning, danger, 'gray';

.gi-tag-dark {
  color: #fff;
  @each $i in $status {
    &.gi-tag-status-#{$i} {
      border: 1px solid rgb(var(--#{$i}-6));
      background-color: rgb(var(--#{$i}-6));
      .gi-tag-close-btn {
        &:hover {
          color: rgb(var(--#{$i}-6));
          &::before {
            background-color: rgb(var(--#{$i}-2));
          }
        }
      }
    }
  }
}

.gi-tag-light {
  color: #fff;
  @each $i in $status {
    &.gi-tag-status-#{$i} {
      color: rgb(var(--#{$i}-6));
      background-color: rgb(var(--#{$i}-1));
      .gi-tag-close-btn {
        &:hover {
          color: #fff;
          &::before {
            background-color: rgb(var(--#{$i}-6));
          }
        }
      }
    }
  }
}

.gi-tag-outline {
  background: transparent;
  @each $i in $status {
    &.gi-tag-status-#{$i} {
      color: rgb(var(--#{$i}-6));
      border: 1px solid rgb(var(--#{$i}-6));
      .gi-tag-close-btn {
        &:hover {
          color: #fff;
          &::before {
            background-color: rgb(var(--#{$i}-6));
          }
        }
      }
    }
  }
}

.gi-tag-light-outline {
  @each $i in $status {
    &.gi-tag-status-#{$i} {
      color: rgb(var(--#{$i}-6));
      border: 1px solid rgb(var(--#{$i}-2));
      background-color: rgb(var(--#{$i}-1));
      .gi-tag-close-btn {
        &:hover {
          color: #fff;
          &::before {
            background-color: rgb(var(--#{$i}-6));
          }
        }
      }
    }
  }
}

.gi-tag-size-mini {
  height: 22px;
  padding: 0 4px;
  .gi-tag-close-btn {
    .close-icon {
      width: 10px;
      height: 10px;
    }
    &::before {
      width: 14px;
      height: 14px;
    }
  }
}

.gi-tag-size-small {
  height: 24px;
}

.gi-tag-size-large {
  height: 28px;
  padding: 0 10px;
  font-size: 14px;
}
</style>
